$(function () {
   getinfo(1);
   $("#filter").click(function () {
       getinfo(1);
   })
})

function getinfo(pagenum) {
    var name=$("#name").val();
    var title=$("#title").val();
    $.post({
        xhrFields: {
            withCredentials: true
        },
        url: baseurl+api.comment.query+pagenum,
        data:{name:name,title:title},
        success:function (res) {
            var s="";
            var commentList=res.data.commentList;
            var pageinfo=res.data.pageinfo;
            for (var i = 0; i < commentList.length; i++) {
                s+="<tr>\n" +
                    "                            <td>"+commentList[i].id+"</td>\n" +
                    "                            <td>"+commentList[i].name+"</td>\n" +
                    "                            <td title='"+commentList[i].content+"'>"+commentList[i].content+"</td>\n" +
                    "                            <td>"+commentList[i].email+"</td>\n" +
                    "                            <td>"+commentList[i].url+"</td>\n" +
                    "                            <td><a href=\"/detail/"+commentList[i].date+"/"+commentList[i].description+"\" class='label label-info'>"+commentList[i].title+"</a></td>\n" +
                    "                            <td>"+commentList[i].time+"</td>\n" +
                    "                            <td><button type=\"button\" class=\"btn btn-primary\" onclick=\"reply("+commentList[i].id+","+commentList[i].userid+",'"+commentList[i].name+"',"+commentList[i].articleid+")\">回复</button>&nbsp;<button class=\"btn-danger btn\" onclick='deletecomments("+commentList[i].id+")'>删除</button></td>\n" +
                    "                        </tr>";
            }
            $("#tbody").html(s);
            $("#current-page").html("第"+pageinfo.pageNum+"页");
            if(!pageinfo.hasPreviousPage){
                $("#first").attr("class","disabled");
                $("#pre").attr("class","disabled");
                $("#first").html("<a href=\"javascript:void(0);\">首页</a>");
                $("#pre").html("<a href=\"javascript:void(0);\" >上一页</a>");
            }else{
                $("#first").removeAttr("class","disabled");
                $("#pre").removeAttr("class","disabled");
                $("#first").html("<a href=\"javascript:void(0);\" onclick=\"getinfo(1)\">首页</a>");
                $("#pre").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageinfo.prePageNum+")\">上一页</a>");
            }
            if(!pageinfo.hasNextPage){
                $("#next").attr("class","disabled");
                $("#last").attr("class","disabled");
                $("#next").html("<a href=\"javascript:void(0);\">下一页</a>");
                $("#last").html("<a href=\"javascript:void(0);\" >尾页</a>");
            }else{
                $("#next").removeAttr("class","disabled");
                $("#last").removeAttr("class","disabled");
                $("#next").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageinfo.nextPageNum+")\">下一页</a>");
                $("#last").html("<a href=\"javascript:void(0);\" onclick=\"getinfo("+pageinfo.totalpage+")\">尾页</a>");
            }
            $("#total").html(pageinfo.totalpage);
        }
    })
}

function reply(id,userid,username,articleid) {
    swal({
        title: '@'+username,
        input: 'text',
        showCancelButton: true,
        inputValidator: function (value) {
            return new Promise(function (resolve, reject) {
                if (value) {
                    resolve()
                } else {
                    reject('请输入要回复的内容!')
                }
            })
        }
    }).then(function (result) {
        $.post({
            url: baseurl+api.comment.reply,
            xhrFields: {
                withCredentials: true
            },
            data:{
                content: result,
                articleId: articleid,
                lastId: id,
                replyUserId: userid
            },
            success: function (msg) {
                swal(msg.message);
                window.location.reload();
            }
        })
    })
}

function deletecomments(id) {
    swal({
        title: '确定删除吗?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定',
        cancelButtonText:"取消",
    }).then(function(isConfirm) {
        if (isConfirm) {
            $.post({
                xhrFields: {
                    withCredentials: true
                },
                url: baseurl+api.comment.delete+id,
                success: function (res) {
                    swal(res.message);
                    setTimeout(function () {
                        window.location.reload();
                    },1000)
                }
            })
        }
    })
}

function transferto() {
    var tale=new $.tale();
    var pagenum=$("#pagenum").val();
    var totalpage=$("#total").html();
    if(pagenum<1||pagenum>totalpage||pagenum==''){
        tale.alertWarn("页码无效!");
        return;
    }
    getinfo(pagenum);
}
